﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using System.Data;

namespace ArcMap_AddIn_Tools
{
    public class FeatureClassInfo
    {
        private Dictionary<IFeatureClass, string> dicLayerInfo = new Dictionary<IFeatureClass, string>();
        private List<IFeatureClass> featureClassList = new List<IFeatureClass>();
        private List<DataTable> dataTableList = new List<DataTable>();
        private IMxDocument mxDocument = ArcMap.Document;

        public List<IFeatureClass> FeatureClassList
        {
            get
            {
                return featureClassList;
            }
        }

        public Dictionary<IFeatureClass, string> DicLayerInfo
        {
            get
            {
                return dicLayerInfo;
            }
        }

        public FeatureClassInfo()
        {
            Initial();
        }

        private void Initial()
        {
            IMap map = mxDocument.FocusMap;
            if (map.LayerCount == 0)
                return;
            //for (int i = 0; i < map.LayerCount; i++)
            //{
            //    ILayer layer = map.get_Layer(i);
            //    IFeatureLayer featureLayer = layer as IFeatureLayer;
            //    IFeatureClass featureClass = featureLayer.FeatureClass;
            //    featureClassList.Add(featureClass);
            //}
            for (int i = 0; i < map.LayerCount; i++)
            {
                ILayer layer = map.get_Layer(i);
                GetLayerInfo(layer, "");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pLayer"></param>
        public void GetLayerInfo(ILayer Layer, string prefix)
        {
            if (Layer as IFeatureLayer != null)
            {
                IFeatureLayer featureLayer = Layer as IFeatureLayer;
                IFeatureClass featureClass = featureLayer.FeatureClass;
                IDataset ds = ((IDataset)featureLayer.FeatureClass);
                string nodeName = prefix + featureLayer.Name;
                dicLayerInfo.Add(featureClass, nodeName);
            }
            else
            {
                if (Layer is IGroupLayer)
                {
                    ICompositeLayer pCompositeLayer = Layer as ICompositeLayer;
                    prefix += Layer.Name + ".";
                    for (int j = 0; j < pCompositeLayer.Count; j++)
                    {                        
                        GetLayerInfo(pCompositeLayer.get_Layer(j), prefix);
                    }
                }
            }
        }

        /// <summary>
        /// 获取要素类数据结构集合
        /// </summary>
        /// <returns></returns>
        public List<DataTable> GetFeatureClassStruct()
        {
            if (dicLayerInfo.Count == 0)
                return null;
            List<DataTable> DataTableList = new List<DataTable>();

            foreach (KeyValuePair<IFeatureClass, string> item in dicLayerInfo)
            {
                IFeatureClass featureClass = item.Key;
                string featureClassName = ((IDataset)featureClass).Name;
                DataTable dt = CreateTable(featureClassName);
                DataRow rows = null;
                IFields fields = featureClass.Fields;
                string[] fieldname = new string[fields.FieldCount];
                string[] Aliasname = new string[fields.FieldCount];
                int[] length = new int[fields.FieldCount];
                IField field = null;
                int num = 1;
                for (int j = 0; j < fields.FieldCount; j++)
                {
                    field = fields.get_Field(j);
                    rows = dt.NewRow();
                    string fieldName = field.Name;
                    if (!fieldName.ToUpper().Contains("SHAPE") && fieldName.ToUpper() != "OBJECTID")
                    {
                        rows["序号"] = num;
                        rows["字段名"] = field.Name;
                        rows["描述"] = field.AliasName;
                        rows["类型"] = getType(field.Type);
                        rows["长度"] = field.Length.ToString();
                        rows["可空"] = field.IsNullable.ToString();
                        rows["默认值"] = field.DefaultValue.ToString();
                        dt.Rows.Add(rows);
                        num++;
                    }
                }
                DataTableList.Add(dt);
            }


            return DataTableList;
        }

        private string getType(esriFieldType fieldType)
        {
            string type = "";
            switch (fieldType)
            {
                case esriFieldType.esriFieldTypeString:
                    type = "文本型";
                    return type;
                case esriFieldType.esriFieldTypeSmallInteger:
                    type = "短整型";
                    return type;
                case esriFieldType.esriFieldTypeDouble:
                    type = "双精度型";
                    return type;
                case esriFieldType.esriFieldTypeDate:
                    type = "日期型";
                    return type;

                case esriFieldType.esriFieldTypeInteger:
                    type = "长整型";
                    return type;
                case esriFieldType.esriFieldTypeBlob:
                    type = "Blob";
                    return type;
                default:
                    return "";
            }
        }

        private DataTable CreateTable(string tablename)
        {
            DataTable dt = new DataTable(tablename);
            DataColumn column = null;
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "序号";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "字段名";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "描述";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "类型";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "长度";
            dt.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "可空";
            dt.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "默认值";
            dt.Columns.Add(column);

            return dt;
        }


    }
}
